package com.yuexunit.mobilenurse.module.BloodManage.presenter.impl;

import com.alibaba.fastjson.JSON;
import com.yuexunit.mobilenurse.config.AppConfig;
import com.yuexunit.mobilenurse.module.BloodManage.bean.CheckDataBean;
import com.yuexunit.mobilenurse.module.BloodManage.model.ICheckModel;
import com.yuexunit.mobilenurse.module.BloodManage.presenter.ICheckPresenter;
import com.yuexunit.mobilenurse.module.BloodManage.ui.view.ICheckView;
import com.yuexunit.mobilenurse.module.Login.bean.YXLoginData;
import com.yuexunit.mobilenurse.util.ProUtil;

import org.json.JSONException;
import org.json.JSONObject;
import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.StringUtils;
import org.slf4j.Logger;

import java.util.Map;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.functions.Func1;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Created by work-jx on 2017/10/24.
 */
public class CheckPresenter implements ICheckPresenter{
    private ICheckView view;
    private ICheckModel model;

    private final Logger log = ProUtil.getLogger(CheckPresenter.class);
    public CompositeSubscription compositeSubscription = new CompositeSubscription();

    public CheckPresenter(ICheckModel model, ICheckView view) {
        this.model = model;
        this.view = view;
    }


    @Override
    public void checkBlood(Map<String, String> praise) {
        model.checkBlood(praise)
                .subscribeOn(Schedulers.io())
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {
                        log.info("UserPresenter_doInLogin_SHOW_DIALOG");
                        view.loadingDialogStatus(AppConfig.SHOW_DIALOG);
                    }
                })
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<CheckDataBean>() {
                               @Override
                               public void onCompleted() {
                                   view.loadingDialogStatus(AppConfig.DISMISS_DIALOG);
                               }

                               @Override
                               public void onError(Throwable e) {
                                   ViewInject.toast("核对失败");
                               }

                               @Override
                               public void onNext(CheckDataBean checkDataBean) {
                                   view.checkBlood(checkDataBean);
                               }
                           }
                );
    }

    @Override
    public void beginBlood(Map<String, String> praise) {
        model.beginBlood(praise)
                .subscribeOn(Schedulers.io())
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {
                        log.info("UserPresenter_doInLogin_SHOW_DIALOG");
                        view.loadingDialogStatus(AppConfig.SHOW_DIALOG);
                    }
                })
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<CheckDataBean>() {
                               @Override
                               public void onCompleted() {
                                   view.loadingDialogStatus(AppConfig.DISMISS_DIALOG);
                               }

                               @Override
                               public void onError(Throwable e) {
                                   ViewInject.toast("开始输血失败");
                               }

                               @Override
                               public void onNext(CheckDataBean checkDataBean) {
                                   view.beginBlood(checkDataBean);
                               }
                           }
                );
    }

    @Override
    public void beginConfirmBlood(Map<String, String> praise) {
        model.beginConfirmBlood(praise)
                .subscribeOn(Schedulers.io())
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {
                        log.info("UserPresenter_doInLogin_SHOW_DIALOG");
                        view.loadingDialogStatus(AppConfig.SHOW_DIALOG);
                    }
                })
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<CheckDataBean>() {
                               @Override
                               public void onCompleted() {
                                   view.loadingDialogStatus(AppConfig.DISMISS_DIALOG);
                               }

                               @Override
                               public void onError(Throwable e) {
                                   ViewInject.toast("开始输血15分钟后确认失败");
                               }

                               @Override
                               public void onNext(CheckDataBean checkDataBean) {
                                   view.beginConfirmBlood(checkDataBean);
                               }
                           }
                );
    }

    @Override
    public void finishBlood(Map<String, String> praise) {
        model.finishBlood(praise)
                .subscribeOn(Schedulers.io())
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {
                        log.info("UserPresenter_doInLogin_SHOW_DIALOG");
                        view.loadingDialogStatus(AppConfig.SHOW_DIALOG);
                    }
                })
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<CheckDataBean>() {
                               @Override
                               public void onCompleted() {
                                   view.loadingDialogStatus(AppConfig.DISMISS_DIALOG);
                               }

                               @Override
                               public void onError(Throwable e) {
                                   ViewInject.toast("结束输血失败");
                               }

                               @Override
                               public void onNext(CheckDataBean checkDataBean) {
                                   view.finishBlood(checkDataBean);
                               }
                           }
                );
    }

    @Override
    public void finishConfirmBlood(Map<String, String> praise) {
        model.finishConfirmBlood(praise)
                .subscribeOn(Schedulers.io())
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {
                        log.info("UserPresenter_doInLogin_SHOW_DIALOG");
                        view.loadingDialogStatus(AppConfig.SHOW_DIALOG);
                    }
                })
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<CheckDataBean>() {
                               @Override
                               public void onCompleted() {
                                   view.loadingDialogStatus(AppConfig.DISMISS_DIALOG);
                               }

                               @Override
                               public void onError(Throwable e) {
                                   ViewInject.toast("结束输血4小时后确认失败");
                               }

                               @Override
                               public void onNext(CheckDataBean checkDataBean) {
                                   view.finishConfirmBlood(checkDataBean);
                               }
                           }
                );
    }

    @Override
    public void cancle(Map<String, String> praise) {
        model.cancle(praise)
                .subscribeOn(Schedulers.io())
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {
                        log.info("UserPresenter_doInLogin_SHOW_DIALOG");
                        view.loadingDialogStatus(AppConfig.SHOW_DIALOG);
                    }
                })
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<CheckDataBean>() {
                               @Override
                               public void onCompleted() {
                                   view.loadingDialogStatus(AppConfig.DISMISS_DIALOG);
                               }

                               @Override
                               public void onError(Throwable e) {
                                   ViewInject.toast("退血失败");
                               }

                               @Override
                               public void onNext(CheckDataBean checkDataBean) {
                                   view.cancle(checkDataBean);
                               }
                           }
                );
    }


    @Override
    public void doInLogin(String uid, String pwd) {
        if (!inputCheck(uid, pwd)) {
            return;
        }

        compositeSubscription.add(
                model.doInLogin(uid, pwd)
                        .subscribeOn(Schedulers.io())
                        .doOnSubscribe(new Action0() {
                            @Override
                            public void call() {
                                log.info("UserPresenter_doInLogin_SHOW_DIALOG");
                                view.loadingDialogStatus(AppConfig.SHOW_DIALOG);
                            }
                        })
                        .subscribeOn(AndroidSchedulers.mainThread())
                        .observeOn(AndroidSchedulers.mainThread())
                        .filter(new Func1<Object, Boolean>() {
                            @Override
                            public Boolean call(Object response) {
                                log.info("UserPresenter_doInLogin_response:" + response);
                                return response != null;
                            }
                        })
                        .filter(new Func1<Object, Boolean>() {
                            @Override
                            public Boolean call(Object response) {

                                JSONObject all, body;
                                String result = null;
                                try {
                                    all = new JSONObject(response.toString());
                                    body = all.getJSONObject("body");
                                    result = body.getString("response");

                                    log.info("UserPresenter_doInLogin_response:" + response.toString());
                                } catch (JSONException e) {
                                    log.error("UserPresenter_doInLogin_e:", e.toString());
                                }

                                if ("null".equals(result)) {
                                    ViewInject.toast("复核账号登录失败");
                                }
                                return !("null".equals(result));
                            }
                        })
                        .map(new Func1<Object, YXLoginData>() {
                            @Override
                            public YXLoginData call(Object response) {
                                YXLoginData Total = JSON.parseObject(response.toString(), YXLoginData.class);
                                YXLoginData loginData = Total.getBody();
                                log.info("UserPresenter_doInLogin:" + loginData.toString());
                                return loginData;
                            }
                        })
                        .filter(new Func1<YXLoginData, Boolean>() {
                            @Override
                            public Boolean call(YXLoginData yxLoginData) {
                                return "0".equals(yxLoginData.getHead().getRet_code());
                            }
                        })
                        .subscribe(new Subscriber<YXLoginData>() {
                            @Override
                            public void onCompleted() {
                                log.info("UserPresenter_doInLogin_onCompleted");
                                view.loadingDialogStatus(AppConfig.DISMISS_DIALOG);
                            }

                            @Override
                            public void onError(Throwable e) {
                                log.info("UserPresenter_doInLogin_onError:" + e);
                                view.loadingDialogStatus(AppConfig.DISMISS_DIALOG);
                                ViewInject.toast("复核账号登录失败");
                            }

                            @Override
                            public void onNext(YXLoginData yxLoginData) {
                                log.info("UserPresenter_doInLogin_onNext:" + yxLoginData.toString());
                                view.doInLogin(yxLoginData);
                            }
                        }));
    }

    //检验账号密码输入格式是否有效
    public boolean inputCheck(String username, String password) {
        if (StringUtils.isEmpty(username)) {
            ViewInject.toast("请输入有效账号");
            return false;
        }

        if (StringUtils.isEmpty(password)) {
            ViewInject.toast("请输入有效密码");
            return false;
        }
        return true;
    }
}
